package com.tbit.uqbike.webmanager.controller;

import com.tbit.uqbike.constant.UserConstant;
import com.tbit.uqbike.object.pojo.SystemMsg;
import com.tbit.uqbike.object.value.LoginInfo;
import com.tbit.uqbike.object.view.JsonResult;
import com.tbit.uqbike.service.business.SystemMsgService;
import com.tbit.uqbike.webmanager.util.Assert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

/**
 * 系统消息
 *
 * @author Leon
 * 2017年2月24日 下午5:51:05
 */
@Validated
@RestController
@RequestMapping(value = "Msg", produces = BaseController.MEDIA_JSON_UTF8)
public class SystemMsgController extends BaseController {
    @Autowired
    private SystemMsgService systemMsgService;

    /* 添加 */
    @RequestMapping("/add")
    public JsonResult add(SystemMsg systemMsg, HttpServletRequest request) {

        /* 校验 */
        Assert.hasLength(systemMsg.getTitle(), "ver.title.notNull");
        Assert.notNull(systemMsg.getStartTime(), "ver.startTime.notNull");
        Assert.notNull(systemMsg.getEndTime(), "ver.endTime.notNull");

        LoginInfo info = getLogin(request);
        if (info.type.equals(UserConstant.USER_ADMIN) || info.type.equals(UserConstant.USER_BRAND)) {
            Assert.notNull(systemMsg.getAccountId(), "ver.accountId.notNull");
        } else {
            systemMsg.setAccountId(info.id);
        }
        systemMsg.setCreateTime(new Date());
        systemMsgService.insert(systemMsg);

        return JsonResult.ok();
    }

    /* 删除 */
    @RequestMapping("/del")
    public JsonResult del(Integer systemMsgId, HttpSession session, HttpServletRequest request, HttpServletResponse response) {
        /* 校验 */
        Assert.notNull(systemMsgId, "ver.systemMsgId.notNull");
        systemMsgService.del(systemMsgId);

        return JsonResult.ok();
    }

    /* 分页查询 */
    @RequestMapping("/queryPage")
    public JsonResult queryPage(Integer accountId,
                                @RequestParam(defaultValue = "10") Integer rowCount,
                                @RequestParam(defaultValue = "1") Integer pageNO,
                                HttpServletRequest request) {
        LoginInfo info = getLogin(request);
        if (info.type.equals(UserConstant.USER_BRAND) || info.type.equals(UserConstant.USER_ADMIN)) {
            Assert.notNull(accountId, "ver.accountId.notNull");
        } else {
            accountId = info.id;
        }
        /*获取总数*/
        Integer total = systemMsgService.queryCount(accountId);
        /*当前返回行数*/
        List<SystemMsg> systemMsgs = systemMsgService.queryPage(accountId, rowCount, pageNO);

        return JsonResult.ok(systemMsgs, total);
    }


}